//
//  XFXReadViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXReadViewController.h"
#import "XFXReadTableViewCell.h"
#import "XFXSubscribeViewController.h"
#import "XFXReadDetailViewController.h"
#import "XFXNewsHeadLineModel.h"
#import "XFXReadModel.h"
#import "XFXAFNAppTools.h"
#import "MJRefresh.h"
@interface XFXReadViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation XFXReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    UINib *nib = [UINib nibWithNibName:@"XFXReadTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
    [self addheader];
    [self addfooter];
    
}

// 下拉刷新
-(void)addheader{
    
    __block XFXReadViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.page = 1;
        vc.isUpLoading = NO;
        [vc getAFNDate:vc.page];
    }];
    
    [self.myTableView headerBeginRefreshing];
}

// 上拉刷新
- (void)addfooter{
    
    __block XFXReadViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        
        vc.page ++;
        if (vc.page <= 9) {
            vc.isUpLoading = YES;
            [vc getAFNDate:vc.page];
        }
        
    }];
}


// AFN数据请求
- (void)getAFNDate:(NSInteger)page{
    
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&size=%ld&passport=&devId=YqAkrCJ3wfEv9ZEe%%2FiPDVw%%3D%%3D&lat=rCFaaLcjf0R02azIDFFLkg%%3D%%3D&lon=GPpvwrRQgY6fDIGbJ1xX8A%%3D%%3D&version=5.4.5&net=wifi&ts=1452000990&sign=MAa5phiwhADNKMOjxF7HAQpIgIt0Bfx4zeqWEmj2yjZ48ErR02zJ6%%2FKXOnxX046I&encryption=1&canal=miliao_news",page * 10];
    [XFXAFNAppTools getNewsWithHeadline:url block:^(id reulst) {
        if (self.isUpLoading==NO) {
            [self.allArray removeAllObjects];
        }
        NSMutableDictionary *dic = reulst;
        NSMutableArray *array = [dic objectForKey:@"推荐"];
        for (NSDictionary *dics in array) {
            XFXNewsHeadLineModel *readModel = [[XFXNewsHeadLineModel alloc] init];
            [readModel setValuesForKeysWithDictionary:dics];
            [self.allArray addObject:readModel];
        }
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        
    }];
    
}

// 分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

// 分行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allArray.count;
    
}

// tableviewcell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XFXReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    XFXNewsHeadLineModel *readModel = [self.allArray objectAtIndex:indexPath.row];
    
    cell.readModel = readModel;
    
    return cell;
    
}

// cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XFXNewsHeadLineModel *readModel = [self.allArray objectAtIndex:indexPath.row];
    XFXReadDetailViewController *readViews = [[XFXReadDetailViewController alloc] init];
    readViews.model = readModel;
    readViews.hidesBottomBarWhenPushed = YES;
    readViews.navigationController.navigationBar.translucent = NO;
    [self.navigationController pushViewController:readViews animated:YES];
    
}

// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200 * XFXSHEI;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
